let VSHADER_SOURCE = `
    attribute vec4 a_Position;
    attribute vec4 a_Color;
    varying vec4 v_Color;
    void main(){
        gl_Position = a_Position;
        gl_PointSize = 10.0;
        v_Color = a_Color;
    }
`;
let FSHADER_SOURCE = `
    precision mediump float;
    uniform vec4 u_FragColor;
    varying vec4 v_Color;
    void main(){
        gl_FragColor =  v_Color;
       
    }
`;
function main() {
    let canvas = document.getElementById('webgl');
    let gl = getWebGLContext(canvas);
    let init = !initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)
    if (init) {
        return console.error('error');
    }
    let n = initVertexBuffers(gl);
    if (n < 0) {
        console.error('Error!');
        return;
    }
    gl.clearColor(0.0, 0.8, 0.8, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);
    // gl.drawArrays(gl.POINTS, 0, n);
    gl.drawArrays(gl.TRIANGLES, 0, n);

}
function initVertexBuffers(gl) {

    let verticesColors = new Float32Array([
        0.0, 0.5, 1.0, 0.0, 0.0,
        -0.5, -0.5, 0.0, 1.0, 0.0,
        0.5, -0.5, 0.0, 0.0, 1.0

    ])


    let n = 3;
    //创建缓冲区
    let vertexColorBuffer = gl.createBuffer();
    if (!vertexColorBuffer) {
        console.error('vertexColorBuffer Error!');
        return -1;
    }
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW);
    let FSIZE = verticesColors.BYTES_PER_ELEMENT;
    let a_Position = gl.getAttribLocation(gl.program, 'a_Position');
    gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 5, 0);
    gl.enableVertexAttribArray(a_Position);

    let a_Color = gl.getAttribLocation(gl.program, 'a_Color');
    gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 5, FSIZE * 2);
    gl.enableVertexAttribArray(a_Color);


    return n;
}


